package temp.others;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Vector;
import javax.management.modelmbean.ModelMBean;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TableFrameDemo extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTable table;
	private JButton btnAdd,btnEdit,btnDel;
	private JScrollPane sPane;
	private JPanel jp;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TableFrameDemo frame = new TableFrameDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	
	
	private Object[] columnNames={"学号","姓名","性别"};
	private Object[][] rowData={
			{"100","李晓峰",true},
			{"101","刘旭冉",false},
			{"103","tom",true},
			{"100","mary",false},
	};
	
	private Vector columnNames1=new Vector();
	private Vector<Vector> rowData1=new Vector<Vector>();
	
	public void initData(){
		columnNames1.add("学号");
		columnNames1.add("姓名");
		columnNames1.add("性别");
		for(int i=0;i<4;i++){
			Vector vector=new Vector<>();			
			vector.add(100);
			vector.add("李晓峰");
			vector.add(false);
			rowData1.add(vector);			
		}
		
	}
	
	private DefaultTableModel model=new DefaultTableModel(rowData, columnNames){
		public Class<?> getColumnClass(int columnIndex){
			return getValueAt(0, columnIndex).getClass();
		 }
		@Override
		public boolean isCellEditable(int row,int column){
			return super.isCellEditable(0, 1);			
		}
	};
	
	public TableFrameDemo() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		jp=new JPanel();
		btnAdd=new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				switch(e.getActionCommand())
				{
				  case "添加":System.out.println("您选择的是添加按钮");break;
				  default:break;
				}
			}
		});
	
		btnEdit=new JButton("编辑");		
		btnEdit.addActionListener(this); 
	

		btnDel=new JButton("删除");
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				switch(e.getActionCommand())
				{
				  case "删除":
				  model.removeRow(table.getSelectedRow());  
				  System.out.println("您选择的是删除按钮");break;
				  default:break;
				}
			}
		});
		jp.add(btnAdd);
		jp.add(btnEdit);
		jp.add(btnDel);
		contentPane.add(jp,BorderLayout.NORTH);
		
		initData();
		sPane=new JScrollPane();
		table=new JTable(model);
		sPane.setViewportView(table);
		contentPane.add(sPane);
		model.addRow(new Object[]{"105","lily",false});
		model.removeRow(1);
	}
	 public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand())
		{
		  case "编辑":if(table.getSelectedRow()>-1){System.out.println("您选择的行号是"+table.getSelectedRow());
		  int row=table.getSelectedRow();
		  System.out.println("此行第一列的数据是"+model.getValueAt(row, 0));}
		  break;
		  default:break;
		}
	}

}
